package org.sanm.domain.company;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sanm.common.Result;
import org.sanm.domain.goods.Goods;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/company")
@Tag(name = "供应商管理", description = "供应商管理")
@RequiredArgsConstructor
public class CompanyController {

    private final CompanyService companyService;

    @PostMapping("add")
    @Operation(summary = "添加供应商", description = "添加供应商")
    public Result<Company> add(@RequestBody Company company){
//        company.setCreateTime(LocalDateTime.now());
//        company.setUpdateTime(LocalDateTime.now());
        boolean save = Db.save(company);

        return Result.success(company);
    }

    @PutMapping("update")
    @Operation(summary = "修改供应商", description = "修改供应商")
    public Result update(@RequestBody Company company){
        Db.updateById(company);
        return Result.success("修改成功");
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除供应商", description = "删除供应商")
    public Result delete(@PathVariable("id") Integer id){

        Db.removeById(id, Company.class);
        return Result.success("删除成功");
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id供应商", description = "根据id供应商")
    public Result<Company> getDetail(@PathVariable("id") Integer id){

        return Result.success(companyService.getDetail(id));
    }

    @GetMapping("ids")
    @Operation(summary = "根据多个id查询供应商", description = "根据多个id查询供应商")
    public Result<List<Company>> getByIds(@RequestParam("ids") List<Integer> ids){

        return Result.success(companyService.getByIds(ids));
    }

    @GetMapping("list")
    @Operation(summary = "分页查询供应商", description = "分页查询供应商")
    public Result<Page<Company>> page(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "provinceCode", required = false) String provinceCode,
            @RequestParam(value = "cityCode", required = false) String cityCode,
            @RequestParam(value = "areaCode", required = false) String areaCode,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){

        Page<Company> page = companyService.page(pageNum, pageSize, name, provinceCode, cityCode, areaCode, startDate, endDate);
        return Result.success(page);
    }
}
